/****** Object:  Stored Procedure [dbo].spChiTietPhieuXuatInsert Script Date: 6/23/2012 ******/
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = object_id(N'[dbo].[spChiTietPhieuXuatInsert]') AND OBJECTPROPERTY(id, N'IsProcedure') = 1)
	DROP PROCEDURE [dbo].[spChiTietPhieuXuatInsert]
GO

/******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:					Description:
**		--------	--------				-----------------------------------
**		23/06/2012	SongTM					Innit created
*******************************************************************************/

CREATE PROCEDURE [dbo].[spChiTietPhieuXuatInsert]
(
	@MaCTPX nvarchar(12) OUTPUT,
	@DonGia int,
	@SoLuong int,
	@GiaTrungThau int,
	@SoPX nvarchar(14),
	@MaLuuKho int
)
WITH ENCRYPTION

AS

SET NOCOUNT ON

if not exists (select * from [tbl_ChiTietPhieuXuat])
	Set @MaCTPX=1
else
	Set @MaCTPX=(select RIGHT(MAX([MaCTPX]),9) from [tbl_ChiTietPhieuXuat])+1
Set @MaCTPX='CTX'+REPLICATE('0',9-LEN(@MaCTPX))+@MaCTPX 

INSERT INTO [tbl_ChiTietPhieuXuat]
(
	[MaCTPX],
	[DonGia],
	[SoLuong],
	[GiaTrungThau],
	[SoPX],
	[MaLuuKho]
)
VALUES
(
	@MaCTPX,
	@DonGia,
	@SoLuong,
	@GiaTrungThau,
	@SoPX,
	@MaLuuKho
)
GO

--GRANT EXECUTE ON [dbo].[spChiTietPhieuXuatInsert] TO [sa]
--GO
